package com.avit.tv.kui.background;

import android.graphics.drawable.GradientDrawable;
import android.view.View;
import androidx.annotation.ColorInt;

/**
 * 自定义drawable圆角背景
 *
 * @author like
 * @date 2020/3/19
 */
public class BgDrawable extends GradientDrawable {

    /**
     * 设置背景圆弧弧度
     * @param radius 弧度大小
     * @return
     */
    public BgDrawable setRadius(float radius) {
        setCornerRadius(radius);
        return this;
    }

    /**
     * 设置背景颜色
     * @param color 颜色
     * @param alpha 透明度 [0~255]
     * @return
     */
    public BgDrawable setBgColor(@ColorInt int color, int alpha) {
        setAlpha(alpha);
        setColor(color);
        return this;
    }

    /**
     * 设置背景颜色，满透明度
     * @param color
     * @return
     */
    public BgDrawable setBgColor(@ColorInt int color) {
        setBgColor(color, 255);
        return this;
    }

    /**
     * 设置默认边框颜色
     * @param strokeColor 边框颜色
     * @return
     */
    public BgDrawable setOutLineStroke(@ColorInt int strokeColor) {
        setOutLineStroke(strokeColor, 2);
        return this;
    }

    /**
     * 设置边框颜色
     * @param strokeColor 边框颜色
     * @param strokWidth 边框宽度
     * @return
     */
    public BgDrawable setOutLineStroke(@ColorInt int strokeColor, int strokWidth) {
        setStroke(strokWidth, strokeColor);
        return this;
    }

    /**
     * 获取圆形背景
     * @return
     */
    public BgDrawable initOralShape() {
        setShape(OVAL);
        return this;
    }

    /**
     * 获取两侧背景为弧形的drawable
     *
     * @param targetView  目标view
     * @param isLandSpace 是否是横向
     * @return
     */
    public BgDrawable initDoubleOralShape(View targetView, boolean isLandSpace) {
        int measuredHeight = targetView.getMeasuredHeight();
        int measuredWidth = targetView.getMeasuredWidth();
        if (isLandSpace) setCornerRadius(measuredHeight / 2f);
        else setCornerRadius(measuredWidth / 2f);
        return this;
    }

    /**
     * 映射到指定的View上
     * @param targetView
     */
    public void targetDrawable(View targetView) {
        targetView.setBackground(this);
        invalidateSelf();
    }

    @Override
    public boolean onLayoutDirectionChanged(int layoutDirection) {
        return true;
    }
}
